package com.example.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.order.client.CourseVoClient;
import com.example.order.client.UcenterMemberClient;
import com.example.order.entity.Order;
import com.example.order.mapper.OrderMapper;
import com.example.order.service.OrderService;
import com.example.order.utils.OrderNoUtil;
import com.example.utils.Result;
import com.example.utils.order.OrderCourse;
import com.example.utils.order.OrderMember;

import io.swagger.annotations.ApiModelProperty;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
	@Autowired
	private CourseVoClient courseVoClient;
	
	@Autowired
	UcenterMemberClient ucenterMemberClient;

	@Override
	public String saveOrder(String courseId, String memberId) {
		// TODO Auto-generated method stub
		
//	    private String id;
//
//	    @ApiModelProperty(value = "订单号")
//	    private String orderNo;
//
//	    @ApiModelProperty(value = "课程id")
//	    private String courseId;
//
//	    @ApiModelProperty(value = "课程名称")
//	    private String courseTitle;
//
//	    @ApiModelProperty(value = "课程封面")
//	    private String courseCover;
//
//	    @ApiModelProperty(value = "讲师名称")
//	    private String teacherName;
//
//	    @ApiModelProperty(value = "会员id")
//	    private String memberId;
//
//	    @ApiModelProperty(value = "会员昵称")
//	    private String nickname;
//
//	    @ApiModelProperty(value = "会员手机")
//	    private String mobile;
//
//	    @ApiModelProperty(value = "订单金额（分）")
//	    private BigDecimal totalFee;
//
//	    @ApiModelProperty(value = "支付类型（1：微信 2：支付宝）")
//	    private Integer payType;
//
//	    private Integer status;
		OrderCourse orderCourse = courseVoClient.getOrderCourse(courseId);
		OrderMember orderMember = ucenterMemberClient.getOrderMember(memberId);
		Order order = new Order();
		String orderNo = OrderNoUtil.getOrderNo();
		order.setOrderNo(orderNo);
		order.setCourseId(orderCourse.getId());
		order.setCourseTitle(orderCourse.getTitle());
		order.setCourseCover(orderCourse.getCover());
		order.setTeacherName(orderCourse.getTeacherName());
		order.setMemberId(orderMember.getId());
		order.setMobile(orderMember.getMobile());
		order.setNickname(orderMember.getNickname());
		order.setStatus(0);
		order.setPayType(1);
		order.setTotalFee(orderCourse.getPrice());
		baseMapper.insert(order);
		
		return order.getOrderNo();

	}

	@Override
	public Integer getStatus(String courseId, String memberId) {
		QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id", memberId);
		queryWrapper.eq("course_id", courseId);


		Order order = baseMapper.selectOne(queryWrapper);
		return order.getStatus();
	}

}
